﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace SMG.Monitor.Domain.Services
{
    /// <summary>
    /// 监视服务
    /// </summary>
    public abstract class MonitorService : IMonitor
    {
        /// <summary>
        /// 是否运行
        /// </summary>
        Boolean isRunFlag;

        /// <summary>
        /// 状态
        /// </summary>
        public StatusEnum Status { get; private set; }

        /// <summary>
        /// 监视配置
        /// </summary>
        protected MonitorSetting monitorSetting { get; private set; }

        /// <summary>
        /// 文件监视器
        /// </summary>
        protected FileSystemWatcher fileWatcher;

        public MonitorService(MonitorSetting monitorSetting) {
            isRunFlag = false;
            Status = StatusEnum.Stop;
            this.monitorSetting = monitorSetting;
        }

        #region Monitor Members

        /// <summary>
        /// 开始
        /// </summary>
        public void Start()
        {
            if (Status == StatusEnum.Stop) {
                Status = StatusEnum.Running;
                fileWatcher = new FileSystemWatcher();
                if (!String.IsNullOrEmpty(monitorSetting.Filter)) 
                    fileWatcher.Filter = monitorSetting.Filter;
                if(monitorSetting.MonitorType.IndexOf("Create")>=0)
                    fileWatcher.Created += OnFileCreatedHandler;
                if(monitorSetting.MonitorType.IndexOf("Update")>=0)
                    fileWatcher.Changed += OnFileUpdatedHandler;
                if(monitorSetting.MonitorType.IndexOf("Delete")>=0)
                    fileWatcher.Deleted += OnFileDeletedHandler;
                fileWatcher.Path = monitorSetting.MonitorPath;
                fileWatcher.EnableRaisingEvents = true;
            }
        }

        /// <summary>
        /// 停止
        /// </summary>
        public void Stop()
        {
            isRunFlag = false;
            Status = StatusEnum.Stop;
            fileWatcher.Dispose();
        }

        #endregion

        /// <summary>
        /// 文件创建方法
        /// 由子类重写
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected virtual void OnFileCreatedHandler(object sender, FileSystemEventArgs e)
        { 
            
        }

        /// <summary>
        /// 文件修改方法
        /// 由子类重写
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected virtual void OnFileUpdatedHandler(object sender, FileSystemEventArgs e) { 
            
        }

        /// <summary>
        /// 文件删除方法
        /// 由子类重写
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected virtual void OnFileDeletedHandler(object sender, FileSystemEventArgs e) { 
            
        }     

    }
}
